package cn.sdhuijin.mediaplatform.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年3月27日 下午2:01:44 类说明
 */
public class DirMaker {

	private static List<String> list;

	/**
	 * Enhancement of java.io.File#createNewFile() Create the given file. If the
	 * parent directory don't exists, we will create them all.
	 * 
	 * @param file
	 *            the file to be created
	 * @return true if the named file does not exist and was successfully
	 *         created; false if the named file already exists
	 * @see java.io.File#createNewFile
	 * @throws IOException
	 */
	public static boolean createFile(File file) {
		boolean flag = false;
		try {
			if (!file.exists()) {
				makeDir(file.getParentFile());
				file.createNewFile();
			}
			flag = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * Enhancement of java.io.File#mkdir() Create the given directory . If the
	 * parent folders don't exists, we will create them all.
	 * 
	 * @see java.io.File#mkdir()
	 * @param dir
	 *            the directory to be created
	 */
	public static void makeDir(File dir) {
		if (!(dir.exists() && dir.isDirectory())) {
			if (!dir.getParentFile().exists()) {
				makeDir(dir.getParentFile());
			}
			dir.mkdir();
		}
	}

	/**
	 * 删除单个文件
	 * 
	 * @param fileName
	 *            要删除的文件的文件名
	 * @return 单个文件删除成功返回true，否则返回false
	 */
	public static boolean deleteFile(String fileName) {
		File file = new File(fileName);
		// 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
		if (file.exists() && file.isFile()) {
			if (file.delete()) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	/**
	 * 查找此目录下视频文件 (支持 3gp,mp4,flv格式)
	 * 
	 * @param url
	 * @return
	 */
	public static List<String> getFiles(String url) {
		list = new ArrayList<String>();
		try {
			File file = new File(url);
			makeDir(file);
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					if (!files[i].isDirectory()) {
						if (files[i].getAbsolutePath().endsWith(".3gp")
								|| files[i].getAbsolutePath().endsWith(".mp4")
								|| files[i].getAbsolutePath().endsWith(".flv")
								|| files[i].getAbsolutePath().endsWith(".mpeg")) {
							list.add(files[i].getAbsolutePath().toString());
							Log.i("查找到视频文件:", files[i].toString());
						}
					}
				}
			} else {
				Log.e("查找视频文件:", "此地址不是文件目录");
			}
		} catch (Exception e) {
			Log.e("查找视频文件出现错误:", "详细信息：" + e.getMessage());
		}
		return list;
	}

	public static void main(String args[]) {
		// String filePath = "C:/temp/a/b/c/d/e/f/g.txt";
		// File file = new File(filePath);
		// try {
		// System.out.println("file.exists()? " + file.exists());
		// boolean created = createFile(file);
		// System.out.println(created ? "File created"
		// : "File exists, not created.");
		// System.out.println("file.exists()? " + file.exists());
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
	}
}
